/**
 * GarinDriver -- Eclipse Public License - v 1.0
 * http://www.eclipse.org/legal/epl-v10.html
 */
package garin.servlet;

import garin.parse.sql.SqlRenderStringBuilder;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet for rendering SQL statements with pretty print and syntax colors.
 */
public class SqlRenderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(final HttpServletRequest rq,
			final HttpServletResponse rp) throws ServletException, IOException {
		rp.sendRedirect("index.jsp");
	}
	
	@Override
	protected void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		try {
			final String dialect = request.getParameter("dialect");
			final String sql = request.getParameter("sql");
			final PrintWriter writer = response.getWriter();
			writer.append("\n<html><head>");
			writer
					.append("\n<link type='text/css' rel='stylesheet' href='sqlFormat.css'>");
			writer.append("\n<style type='text/css'>");
			writer.append("\na:hover {");
			writer.append("\nborder: 1px dotted red;");
			writer.append("\n}");
			writer.append("\n</style>");
			writer.append("\n<script src='script.js'></script></head><body>");
			writer.append("\n[<a href='index.jsp'>return to edit page</a>]");
			writer.append("\n<p/><hr/>");
			writer.write(new SqlRenderStringBuilder(sql, dialect).get());
			writer.append("\n</body></html>");
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}
}
